package com.example.administrator.caigou51.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.bean.GoodsDetailBean;
import com.example.administrator.caigou51.bean.GoodsSayBean;
import com.example.administrator.caigou51.bean.TradeBean;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.SuperDIYSwipeRefreshLayout;
import com.example.administrator.caigou51.recyclerCard.card.GoodsSayBooksCard;
import com.example.administrator.caigou51.recyclerCard.card.TradeRecordCard;
import com.example.administrator.caigou51.recyclerCard.card.ZhuangQuCard2;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;


/**
 * Created by cjy on 2015/10/3.
 */
@SuppressLint("ValidFragment")
public class SayBooksFragment extends BaseFragment implements View.OnClickListener {
    private View holderView;
    private boolean isScaled;

    private TextView textViewShuoMingShu;
    private TextView textViewJiaoYi;
    private TextView textViewGuanlian;

    private View viewShuoMingShuLine;
    private View viewJiaoYiLine;
    private View viewGuanLianLine;
    private LinearLayout linearLayoutEmpty;
    private MaterialListView materialListView;
    private SuperDIYSwipeRefreshLayout superDIYSwipeRefreshLayout;
    private String itemid;
    private GoodsSayBean goodsSayBean;
    private List<TradeBean> tradeBeanList;
    private List<GoodsDetailBean> goodsBeanList;
    public SayBooksFragment(String itemid) {
        super();
        this.itemid=itemid;
    }
    public SayBooksFragment() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.fragment_say_book, container, false);
            textViewShuoMingShu = (TextView) holderView.findViewById(R.id.textViewShuoMingShu);textViewShuoMingShu.setOnClickListener(this);
            textViewJiaoYi = (TextView) holderView.findViewById(R.id.textViewJiaoYi);textViewJiaoYi.setOnClickListener(this);
            textViewGuanlian = (TextView) holderView.findViewById(R.id.textViewGuanlian);textViewGuanlian.setOnClickListener(this);
            linearLayoutEmpty = (LinearLayout) holderView.findViewById(R.id.linearLayoutEmpty);
            viewShuoMingShuLine=holderView.findViewById(R.id.viewShuoMingShuLine);
            viewJiaoYiLine=holderView.findViewById(R.id.viewJiaoYiLine);
            viewGuanLianLine=holderView.findViewById(R.id.viewGuanLianLine);
            materialListView = (MaterialListView) holderView.findViewById(R.id.materialListView);
            superDIYSwipeRefreshLayout=(SuperDIYSwipeRefreshLayout) holderView.findViewById(R.id.SuperDIYSwipeRefreshLayout);
            superDIYSwipeRefreshLayout.setCanRefresh(false);
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }
        initDatas();
        TaskGetGoodsSay();
        return holderView;
    }



    protected void initDatas() {
        linearLayoutEmpty.setVisibility(View.GONE);
        changeTextViewColor(textViewShuoMingShu,viewShuoMingShuLine);

    }


    protected void fillArrayInListView(GoodsSayBean goodsSayBean) {
        GoodsSayBooksCard goodsSayBooksCard=new GoodsSayBooksCard(getActivity());
        goodsSayBooksCard.setGoodsSayBean(goodsSayBean);
        materialListView.add(goodsSayBooksCard);
    }



    private void TaskGetGoodsSay() {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), materialListView, false, null,
                Constant.getRootUrl() + Constant.Action.Action_GoodsSay, Constant.Action.Action_GoodsSay, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_GoodsSay,itemid), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();

                        goodsSayBean  = JSON.parseObject(result.getData().toString(), GoodsSayBean.class);


                        fillArrayInListView(goodsSayBean);

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    private void TaskGetTradeBean(boolean isLoadMore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), materialListView, false, superDIYSwipeRefreshLayout,
                Constant.getRootUrl() + Constant.Action.Action_TradeRecord, Constant.Action.Action_TradeRecord, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_TradeRecord,superDIYSwipeRefreshLayout.getPageSize()+"",superDIYSwipeRefreshLayout.getPage(isLoadMore)+"",itemid), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();

                        if (result.getData()!=null) {
                            tradeBeanList = JSON.parseArray(result.getData().toString(), TradeBean.class);
                            if ((tradeBeanList!=null && tradeBeanList.size()==0)  || tradeBeanList==null )
                            {
                                linearLayoutEmpty.setVisibility(View.VISIBLE);
                            }else
                            {
                                linearLayoutEmpty.setVisibility(View.GONE);
                            }
                            for (int i = 0; i < tradeBeanList.size(); i++) {
                                TradeRecordCard tradeRecordCard = new TradeRecordCard(getActivity());
                                tradeRecordCard.setTradeBean(tradeBeanList.get(i));
                                if (i == 0) {
                                    tradeRecordCard.setFirst(true);
                                }
                                materialListView.add(tradeRecordCard);
                            }
                        }

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }

    private void TaskGetGuanLianGoods() {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), materialListView, false, null,
                Constant.getRootUrl() + Constant.Action.Action_RelatedGoods, Constant.Action.Action_RelatedGoods, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_RelatedGoods,itemid), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();

                        if (result.getData()!=null) {
                            goodsBeanList = JSON.parseArray(result.getData().toString(), GoodsDetailBean.class);
                            if ((goodsBeanList!=null && goodsBeanList.size()==0)  || goodsBeanList==null )
                            {
                                linearLayoutEmpty.setVisibility(View.VISIBLE);
                            }else
                            {
                                linearLayoutEmpty.setVisibility(View.GONE);
                            }
                            for (int i = 0; i < goodsBeanList.size(); i++) {
                                ZhuangQuCard2 zhuangQuCard2 = new ZhuangQuCard2(getActivity());
                                zhuangQuCard2.setGoodsBean(goodsBeanList.get(i));

                                materialListView.add(zhuangQuCard2);
                            }
                        }

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    private void changeTextViewColor(TextView textView,View viewLine)
    {
        textViewJiaoYi.setTextColor(getResources().getColor(R.color.color_666666));
        textViewGuanlian.setTextColor(getResources().getColor(R.color.color_666666));
        textViewShuoMingShu.setTextColor(getResources().getColor(R.color.color_666666));
        viewGuanLianLine.setVisibility(View.INVISIBLE);
        viewJiaoYiLine.setVisibility(View.INVISIBLE);
        viewShuoMingShuLine.setVisibility(View.INVISIBLE);
        textView.setTextColor(getResources().getColor(R.color.color_1ca146));
        viewLine.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.textViewShuoMingShu:
                materialListView.clear();
                changeTextViewColor(textViewShuoMingShu,viewShuoMingShuLine);
                if (goodsSayBean==null) {
                  linearLayoutEmpty.setVisibility(View.VISIBLE);
                  TaskGetGoodsSay();
                }else
                {

                    linearLayoutEmpty.setVisibility(View.GONE);
                    GoodsSayBooksCard goodsSayBooksCard = new GoodsSayBooksCard(getActivity());
                    goodsSayBooksCard.setGoodsSayBean(goodsSayBean);
                    materialListView.add(goodsSayBooksCard);
                }
                break;
            case R.id.textViewJiaoYi:
                materialListView.clear();
                //如果数据为空就请求
                changeTextViewColor(textViewJiaoYi,viewJiaoYiLine);
                if (tradeBeanList==null) {
                    linearLayoutEmpty.setVisibility(View.VISIBLE);
                    TaskGetTradeBean(false);
                }else
                {
                    if (tradeBeanList.size()==0)
                    linearLayoutEmpty.setVisibility(View.VISIBLE);
                    else
                        linearLayoutEmpty.setVisibility(View.GONE);
                    for (TradeBean tradeBean : tradeBeanList)
                    {
                        TradeRecordCard tradeRecordCard=new TradeRecordCard(getActivity());
                        tradeRecordCard.setTradeBean(tradeBean);
                        materialListView.add(tradeRecordCard);
                    }

                }
                break;
            case R.id.textViewGuanlian:
                changeTextViewColor(textViewGuanlian,viewGuanLianLine);
                materialListView.clear();
                if (goodsBeanList==null) {
                    linearLayoutEmpty.setVisibility(View.VISIBLE);
                    TaskGetGuanLianGoods();

                }else
                {
                    if (goodsBeanList.size()==0)
                        linearLayoutEmpty.setVisibility(View.VISIBLE);
                    else
                        linearLayoutEmpty.setVisibility(View.GONE);
                    for (GoodsDetailBean goodsBean : goodsBeanList)
                    {
                        ZhuangQuCard2 zhuangQuCard2 = new ZhuangQuCard2(getActivity());
                        zhuangQuCard2.setGoodsBean(goodsBean);
                        materialListView.add(zhuangQuCard2);
                    }

                }


                break;
        }
    }
}
